package com.cn.wanxi.data.impl;

import com.cn.wanxi.data.ProductData;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductData {
    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = "select * from sys_product " +
                "where true ";
        if (!"".equals(productModel.getProductName())) {
            sql += "and name like '%" + productModel.getProductName() + "%' ";
        }
        if (!"".equals(productModel.getDescribe())&&productModel.getDescribe()!=null) {
            sql += "and describe like '%" + productModel.getDescribe() + "%'";
        }
        if (!"".equals(productModel.getMold())) {
            sql += "and mold like '%" + productModel.getMold() + "%'";
        }
        if (!"".equals(productModel.getLevel())) {
            sql += "and level like '%" + productModel.getMold() + "%'";
        }
//        取得价格
        if (!"".equals(productModel.getLowPrice()) && "".equals(productModel.getTopPrice())) {
            sql += " and price >='" + productModel.getLowPrice() + "'";
        }
        if ("".equals(productModel.getLowPrice()) && !"".equals(productModel.getTopPrice())) {
            sql += " and price <='" + productModel.getTopPrice() + "'";
        }
        if (!"".equals(productModel.getLowPrice()) && !"".equals(productModel.getTopPrice())) {
            sql += " and price between '" + productModel.getLowPrice() + "' and '" + productModel.getTopPrice() + "'";
        }
//        取得重量
        if (!"".equals(productModel.getLowWeight()) && "".equals(productModel.getTopWeight())) {
            sql += " and weight >='" + productModel.getLowWeight() + "'";
        }
        if ("".equals(productModel.getLowWeight()) && !"".equals(productModel.getTopWeight())) {
            sql += " and weight <='" + productModel.getTopWeight() + "'";
        }
        if (!"".equals(productModel.getLowWeight()) && !"".equals(productModel.getTopWeight())) {
            sql += " and weight between '" + productModel.getLowWeight() + "' and '" + productModel.getTopWeight() + "'";
        }
//        if (userModel.getEnable() != -1 && userModel.getEnable() != null){
//            if (userModel.getEnable() == 1){
//                sql +=" and enable=1";
//            }else{
//                sql +="and enable!=1";
//            }
//        }
        sql += "order by productid desc";
        ResultSet resultSet = JDBC.query(sql);
        return setResultData(resultSet);
    }

    @Override
    public ProductModel findById(Integer productid) {
        String sql = "select * from sys_product where productid=" + productid;
        ResultSet resultSet = JDBC.query(sql);
        List<ProductModel> list = setResultData(resultSet);
        return list.size() > 0 ? list.get(0) : null;
    }
    @Override
    public int edit(ProductModel productModel) {
        String sql = "update sys_product set name='" + productModel.getProductName() + "'where productid =" + productModel.getProductId();
        return JDBC.excute(sql);
    }

    @Override
    public int Adding(ProductModel productModel) {
        String sql="INSERT INTO `sys`.`sys_product` " +
                "`name`, " +
                "`describe`, " +
                "`mold`, " +
                "`level`, " +
                "`weight`, " +
                "`price`)" +
                " VALUES ('"+productModel.getProductName()+"', " +
                "'"+productModel.getDescribe()+"', " +
                "'"+productModel.getMold()+"', " +
                "'"+productModel.getLevel()+"', " +
                "'"+productModel.getProductWeight()+"', " +
                "'"+productModel.getPrice()+"' ";
        return JDBC.excute(sql);
    }

    @Override
    public int del(Integer id) {
        String sql = "delete from sys_product where productid =" + id;

        return JDBC.excute(sql);
    }

    private List<ProductModel> setResultData(ResultSet resultSet) {
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setProductId(resultSet.getInt("productid"));
                model.setProductName(resultSet.getString("name"));
                model.setDescribe(resultSet.getString("describe"));
                model.setMold(resultSet.getString("mold"));
                model.setLevel(resultSet.getString("level"));
                model.setProductWeight(resultSet.getString("weight"));
                model.setPrice(resultSet.getString("price"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
//    String sql = "INSERT INTO `sys`.`sys_user` \n" +
//            "( `name`," +
//            " `describe`," +
//            " `mold`," +
//            " `level`," +
//            " `weight`," +
//            " `price`," +
//            "`create_time`)\n" +
//            " VALUES ( '" + productModel.getProductName() + "'," +
//            " '" + productModel.getDescribe() + "'," +
//            " '" + productModel.getMold() + "'," +
//            " '" + productModel.getLevel() + "'," +
//            " '" + productModel.getProductWeight() + "'," +
//            " '" + productModel.getPrice() + "'," +
//            "now())";

}
